package com.industrial.im.controller;

import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.im.domain.req.GroupReq;
import com.industrial.im.service.IMsgGroupInfoService;
import com.industrial.im.service.ImGroupService;
import com.industrial.system.api.domain.SysUser;
import io.rong.models.Result;
import io.rong.models.response.UserGroupQueryResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/im/group")
public class ImGroupController {

    @Autowired
    private ImGroupService imGroupService;

    @Autowired
    private IMsgGroupInfoService iMsgGroupInfoService;

    /**
     * <p>
     * 创建群组
     * </p>
     *
     * @param groupReq
     * @return
     */
    @PostMapping("/createImGroup")
    public AjaxResult createImGroup(@RequestBody GroupReq groupReq) {
        if (StringUtils.isBlank(groupReq.getGroupName())) {
            return AjaxResult.warn("请输入群组名称");
        }

        int count = iMsgGroupInfoService.selectGroupNameCount(groupReq.getGroupName());
        if (count > 0) {
            return AjaxResult.warn("该群组名已存在，请更换其他名称");
        }
        Result imGroup = imGroupService.createImGroup(groupReq);
        return AjaxResult.success(imGroup);
    }

    /**
     * <p>
     * 查询用户所在的群组
     * </p>
     *
     * @return
     */
    @GetMapping("/queryUserInGroup/{userId}")
    public AjaxResult queryUserInGroup(@PathVariable("userId") String userId) {
        UserGroupQueryResult userGroupQueryResult = imGroupService.queryUserInGroup(userId);
        return AjaxResult.success(userGroupQueryResult);
    }

    /**
     * <p>
     * 查询群组内成员
     * </p>
     *
     * @return
     */
    @GetMapping("/queryGroupMembers/{groupId}")
    public AjaxResult queryGroupMembers(@PathVariable("groupId") String groupId) {
        List<SysUser> sysUsers = imGroupService.queryGroupMembers(groupId);
        return AjaxResult.success(sysUsers);
    }

    /**
     * <p>
     * 解散群组
     * </p>
     *
     * @return
     */
    @PostMapping("/dissolveGroup")
    public AjaxResult dissolveGroup(@RequestBody Map<String, String> params) {
        if (StringUtils.isBlank(params.get("groupId")) || StringUtils.isBlank(params.get("userId"))) {
            return AjaxResult.warn("请传入群组或用户ID");
        }
        Result result = imGroupService.dissolveGroup(params);
        return AjaxResult.success(result);
    }

    /**
     * <p>
     * 邀请用户入群
     * </p>
     *
     * @param groupReq
     * @return
     */
    @PostMapping("/invitingUsersJoinGroup")
    public AjaxResult invitingUsersJoinGroup(@RequestBody GroupReq groupReq) {
        if (StringUtils.isBlank(groupReq.getGroupId())) {
            return AjaxResult.warn("请传入组ID");
        }
        if (StringUtils.isBlank(groupReq.getGroupName())) {
            return AjaxResult.warn("请传入组名称");
        }
        List<String> userIds = groupReq.getUserIds();
        if (userIds == null || userIds.isEmpty()) {
            return AjaxResult.warn("请传入邀请的用户ID");
        }
        Result result = imGroupService.invitingUsersJoinGroup(groupReq);
        return AjaxResult.success(result);
    }

    /**
     * <p>
     * 移除群中的成员
     * </p>
     *
     * @param params userId 被移除的用户，groupId 群组Id
     * @return
     */
    @PostMapping("/removeGroupUser")
    public AjaxResult removeGroupUser(@RequestBody Map<String, String> params) {
        if (StringUtils.isBlank(params.get("groupId")) || StringUtils.isBlank(params.get("userId"))) {
            return AjaxResult.warn("请传入群组ID或用户ID");
        }
        Result result = imGroupService.removeGroupUser(params);
        return AjaxResult.success(result);
    }
}
